<?php
/**
 * @license http://www.gnu.org/licenses/lgpl.html LGPL
 * @author Harlan Iverson <h dot iverson at gmail dot com>
 * @copyright Copyright (c) 2006, Harlan Iverson.
 * @version $Id$
 */

require_once( "Pearified/Smarty/Smarty.class.php" );

require_once( "PhpFaces/Context/FacesContext.php" );
require_once( "PhpFaces/Application/ViewHandler.php" );
require_once( "PhpFaces/Component/UIComponent.php" );

// REMOVE just for testing purposes //

/**
 * @return PhpFaces_Component_UIViewRoot
 */
class PhpFaces_ViewHandler_HardCore extends PhpFaces_Application_ViewHandler {
	public function createView( PhpFaces_Context_FacesContext $facesContext, $viewId ) {
		
		/*
		$resolver = new PhpFaces_Application_ResourceResolver();
		$file = $resolver->resolveUrl( $viewId );
		
		if( !file_exists( $file ) ) {
			throw new Exception( "TODO:type me. Resource '$viewId' doesn't exist. (it maps to $file)'" );
		}
		*/
		
		$facesContext->setRenderKitId( PhpFaces_Render_RenderKitFactory::HTML_BASIC_RENDER_KIT );
		$application = $facesContext->getApplication();
		
		$viewRoot = $application->createComponent( "javax.faces.ViewRoot" );
		$viewRoot->setRenderKitId( $facesContext->getRenderKitId() );
		$viewRoot->setViewId( $viewId );
		
		require( $viewId );

		return $viewRoot;
		
	}
	public function renderView( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIViewRoot $viewToRender ) {
		//print_r( $viewToRender );

		$writer = new XMLWriter();
		$writer->openMemory();
		$facesContext->setResponseWriter( $writer );
		
		$writer->startDocument();
		//$writer->startDocument( PhpFaces_XHTML::XML_VERSION, PhpFaces_XHTML::ENCODING );
		
		PhpFaces_ComponentSupport::encodeRecursive( $facesContext, $viewToRender );
		
		$writer->endDocument();
		
		$output = $writer->outputMemory( true );
		
		$outputStream = $facesContext->getExternalContext()->getResponse()->getOutputStream();
		fwrite( $outputStream, $output );
		fflush( $outputStream );
		//fclose( $outputStream );
	}
	

}

class PhpFaces_ComponentSupport {
	/**
	 * Recursively encode a view root, rendering children if the component itself doesn't take care of it.
	 */
	public static function encodeRecursive( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIComponent $viewToRender ) {
		if( $viewToRender->isRendered() ) {
			$viewToRender->encodeBegin( $facesContext );
			if( $viewToRender->getRendersChildren() ) {
				$viewToRender->encodeChildren( $facesContext );
			} else {
				$children = $viewToRender->getChildren();
				// FIXME actually fix the PhpFaces_Lang_ListObject...
				foreach( $children->asArray() as $child ) {
					self::encodeRecursive( $facesContext, $child );
				}
			}
			$viewToRender->encodeEnd( $facesContext );
		}
	}
}

class PhpFaces_ViewHandler_Smarty extends PhpFaces_Application_ViewHandler {
	
	private $smarty;
	
	public function createView( PhpFaces_Context_FacesContext $facesContext, $viewId ) {
		
		// TODO this class should be configurable through web.xml
		$resolver = new PhpFaces_Application_ResourceResolver();
		$file = $resolver->resolveUrl( $viewId );
		
		$smarty = $this->getSmarty();
		$smarty->fetch( $file );
	}
	public function renderView( PhpFaces_Context_FacesContext $facesContext, PhpFaces_Component_UIViewRoot $viewToRender ) {
		
	}
	
	// the Phasecelts ViewHandler will simply override this function
	protected function getSmarty() {
			if( $this->smarty == null ) {
				$smarty = new Smarty();
				
				// TODO register any block function handlers, etc, here
				$this->smarty = $smarty;
			}
			
			return $this->smarty;
	}
}

class PhpFaces_ViewHandler_Phacelets extends PhpFaces_ViewHandler_Smarty {
	private $smarty;
	
	
	protected function getSmarty() {	
		if( $this->smarty == null ) {
			$smarty = parent::getSmarty();
			
			// TODO register additional crap here (phacelets resource handler)
			
			$this->smarty = $smarty;
		}
		
		
		return $this->smarty ;
	}
}






// =================== Components ===========================












?>